import 'package:flutter/cupertino.dart';
import 'package:go_router/go_router.dart';

import '../features/main/view/main_page.dart';
import '../features/room_list/view/room_list_page.dart';
import '../features/splash/view/splash_page.dart';
import '../features/video_list/view/video_list_page.dart';
import '../features/video_play/view/video_play_page.dart';
import '../features/video_tag/view/video_tag_page.dart';

class NavigationUtil {
  static final NavigationUtil _instance = NavigationUtil._internal();
  final GlobalKey<NavigatorState> _key = GlobalKey<NavigatorState>();

  factory NavigationUtil() => _instance;

  NavigationUtil._internal();

  GoRouter getRouterConfig() {
    return GoRouter(navigatorKey: _key, initialLocation: '/splash_page', routes: _registerRoute());
    // return GoRouter(navigatorKey: _key, initialLocation: '/video_play_page', routes: _registerRoute());
  }

  ///app注册所有路由的方法
  List<RouteBase> _registerRoute() {
    return <RouteBase>[
      GoRoute(
        path: '/splash_page',
        builder: (BuildContext context, GoRouterState state) {
          return SplashPage();
        },
      ),
      GoRoute(
        path: '/main_page',
        builder: (BuildContext context, GoRouterState state) {
          return MainPage();
        },
      ),
      GoRoute(
        path: '/room_list_page',
        builder: (BuildContext context, GoRouterState state) {
          final title = state.uri.queryParameters['title'] ?? '';
          return RoomListPage(title: title);
        },
      ),
      GoRoute(
        path: '/video_tag_page',
        builder: (BuildContext context, GoRouterState state) {
          final json = state.uri.queryParameters['json'] ?? '';
          return VideoTagPage(json: json);
        },
      ),

      GoRoute(
        path: '/video_list_page',
        builder: (BuildContext context, GoRouterState state) {
          final json = state.uri.queryParameters['json'] ?? '';
          return VideoListPage(json: json);
        },
      ),

      GoRoute(
        path: '/video_play_page',
        builder: (BuildContext context, GoRouterState state) {
          final videoName = state.uri.queryParameters['videoName'] ?? '';
          final videoUrl = state.uri.queryParameters['videoUrl'] ?? '';
          return VideoPlayPage(videoName: videoName, videoUrl: videoUrl);
        },
      ),
    ];
  }

  ///跳转到首页
  void goMainPage() {
    _key.currentContext?.pushReplacement('/main_page');
  }

  ///跳转到房间列表页面
  void goRoomListPage(String title) {
    _key.currentContext?.push('/room_list_page?title=$title');
  }

  ///跳转到视频标签页
  void goVideoTagPage(String json) {
    _key.currentContext?.push('/video_tag_page?json=$json');
  }

  ///跳转到视频列表页
  void goVideoListPage(String json) {
    _key.currentContext?.push('/video_list_page?json=$json');
  }

  ///跳转到视频播放页
  void goVideoPlayPage(String videoName, String videoUrl) {
    _key.currentContext?.push('/video_play_page?videoName=$videoName&videoUrl=$videoUrl');
  }
}

// 全局实例
final navigationUtil = NavigationUtil();
